package com.surekam.dma.service.dma;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;

import com.surekam.dma.dao.dma.SubjectDao;
import com.surekam.dma.entity.dma.Subject;

/**
 * 安全相关实体的管理类, 包括主题.
 * 
 * @author liuhua
 */
//Spring Bean的标识.
@Component
//默认将类中的所有函数纳入事务管理.
@Transactional
public class SubjectManager {

	private static Logger logger = LoggerFactory.getLogger(SubjectManager.class);

	private SubjectDao subjectDao;

	/**
	 * 使用属性过滤条件查询用户.
	 */
	@Transactional(readOnly = true)
	public Page<Subject> searchSubject(final Page<Subject> page, final List<PropertyFilter> filters) {
		return subjectDao.findPage(page, filters);
	}

	@Transactional(readOnly = true)
	public Subject findSubjectByTitle(String title) {
		return subjectDao.findUniqueBy("title", title);
	}

	public Subject getSubject(Long id){
		return subjectDao.get(id);
	}
	
	public void saveSubject(Subject entity) {
		subjectDao.save(entity);
	}

	public void deleteSubject(Long id) {
		logger.warn("删除主题 " + id, SpringSecurityUtils.getCurrentUserName());
		subjectDao.delete(id);
	}

	//-- Authority Manager --//
	@Transactional(readOnly = true)
	public List<Subject> getAllSubject() {
		return subjectDao.getAll();
	}

	@Autowired
	public void setSubjectDao(SubjectDao subjectDao){
		this.subjectDao = subjectDao;
	}
}
